package fr.utt.lo02.CrazyEights.model.strategies;

import java.io.Serializable;
import java.util.Hashtable;

import fr.utt.lo02.CrazyEights.model.Game;
import fr.utt.lo02.CrazyEights.model.cards.Card;
import fr.utt.lo02.CrazyEights.model.cards.CardSuit;
import fr.utt.lo02.CrazyEights.model.players.Player;



/**
 * This strategy try to get rid of the most valuable Cards
 * the Player has in Hand.
 */
public class StrategyScore implements Strategy,Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * We choose the Suit for which we have the most
	 * valuable Cards in the hand of the Player.
	 */
	public CardSuit chooseSuit(Player player) {
		Hashtable <CardSuit, Integer> hist = new Hashtable <CardSuit, Integer>();
		CardSuit suitToPlay = null;
		int pointsMax = 0;
		
		// Initializing the history table
		for(CardSuit t : CardSuit.values())
			hist.put(t, 0);

		// filling the history table
		for(Card c : player.getCardsInHand())
			hist.put(c.getSuit(), hist.get(c.getSuit()) + c.getRank().getPoints());
				// We add the value of the Card to the history table w/ the index
				// corresponding to the Card's Suit

		// finding the Suit which is the more valuable
		for(CardSuit t : CardSuit.values())
			if(hist.get(t) > pointsMax) {
				pointsMax = hist.get(t);
				suitToPlay = t;
			}

		return suitToPlay;
	}

	/**
	 * We choose the most valuable Card in the hand of the Player.
	 */
	public Card play(Player player) {
		int pointsOfTheCard = 0;
		Card card = null;

			// we try to find the most valuable Card
			for(Card c : player.getCardsInHand())
				if(c.getRank().getPoints() > pointsOfTheCard &&
						Game.getInstance().getDiscardPile().isPlayable(c)) {
					pointsOfTheCard = c.getRank().getPoints();
					card = c;
				}

		return card;
	}

}
